﻿using Inovout.Net;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace Inovout.Data.Redis
{
    public class RedisCommandScriptExecutor
    {
        internal List<RedisCommandScript> CommandScripts { get; private set; }
        public RedisConnection Connection
        {
            get;
            private set;
        }
        public RedisCommandScriptExecutor(RedisConnection connection)
        {
            Connection = connection;
            CommandScripts = new List<RedisCommandScript>();
        }
        internal virtual void Execute(RedisCommandScript commandScript)
        {
            CommandScripts.Add(commandScript);
            if (!Connection.IsTransactionConnection)
            {
                Flush();
            }
        }

        internal void Flush()
        {
            //先实现基于http
            HttpClient client = new HttpClient(new Uri(Connection.ConnectionString, "Command/" + CommandScripts.Count.ToString()));

            HttpRequest request = client.CreateRequest();
            Stream stream = request.GetStream();
            //List<byte> buffers = new List<byte>();
            //buffers.AddRange(System.Text.Encoding.UTF8.GetBytes("*3"));
            //buffers.AddRange(RedisCommandScript.CrlfButffer);
            //buffers.AddRange(System.Text.Encoding.UTF8.GetBytes("$3"));
            //buffers.AddRange(RedisCommandScript.CrlfButffer);
            //buffers.AddRange(System.Text.Encoding.UTF8.GetBytes("set"));
            //buffers.AddRange(RedisCommandScript.CrlfButffer);
            //buffers.AddRange(System.Text.Encoding.UTF8.GetBytes("$3"));
            //buffers.AddRange(RedisCommandScript.CrlfButffer);
            //buffers.AddRange(System.Text.Encoding.UTF8.GetBytes("foo"));
            //buffers.AddRange(RedisCommandScript.CrlfButffer);
            //buffers.AddRange(System.Text.Encoding.UTF8.GetBytes("$11"));
            //buffers.AddRange(RedisCommandScript.CrlfButffer);
            //buffers.AddRange(System.Text.Encoding.UTF8.GetBytes("hello wordd"));
            //buffers.AddRange(RedisCommandScript.CrlfButffer);
            //buffers.AddRange(System.Text.Encoding.UTF8.GetBytes("*2"));
            //buffers.AddRange(RedisCommandScript.CrlfButffer);
            //buffers.AddRange(System.Text.Encoding.UTF8.GetBytes("$3"));
            //buffers.AddRange(RedisCommandScript.CrlfButffer);
            //buffers.AddRange(System.Text.Encoding.UTF8.GetBytes("get"));
            //buffers.AddRange(RedisCommandScript.CrlfButffer);
            //buffers.AddRange(System.Text.Encoding.UTF8.GetBytes("$3"));
            //buffers.AddRange(RedisCommandScript.CrlfButffer);
            //buffers.AddRange(System.Text.Encoding.UTF8.GetBytes("foo"));
            //buffers.AddRange(RedisCommandScript.CrlfButffer);
            //stream.Write(buffers.ToArray(), 0, buffers.Count);
            //string a = @"*3\r\n$3\r\nset\r\n$3\r\nfoo\r\n$11\r\nhello world\r\n*2\r\n$3\r\nget\r\n$3\r\nfoo\r\n";


            for (int i = 0; i < CommandScripts.Count; i++)
            {
                CommandScripts[i].Write(stream);
            }
            client.Send(request,new RedisReplyParser(CommandScripts.ToArray()));
        }
    }
}
